package org.iteam.core.service.oracle.impl.sys;

import java.sql.SQLException;
import java.util.List;

import org.iteam.core.model.sys.SysFunction;
import org.iteam.core.service.DBService;
import org.iteam.core.service.sys.SysFunctionService;
import org.iteam.core.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;

public class SysFunctionServiceImpl extends SysFunctionService {

	@Override
	public List<SysFunction> queryByPage(long page, long rows)
			throws SQLException {
		return super.query(
				DBService.toOraclePage(
						"select * from " + SysFunction.TABLE_NAME
								+ " order by level,code ,create_time desc")
						.toUpperCase(), page * rows, rows * (page - 1));
	}

	@Override
	public List<SysFunction> queryByPage(long page, long rows, String parent)
			throws SQLException {
		if (parent == null || "".equals(parent))
			return super
					.query(DBService
							.toOraclePage(
									"select * from SYS_FUNCTION WHERE parent is null  order by sort DESC,code ,create_time desc")
							.toUpperCase(), page * rows, rows * (page - 1));
		else

			return super
					.query(DBService
							.toOraclePage(
									"select * from SYS_FUNCTION WHERE parent = ?  order by sort DESC,code ,create_time desc")
							.toUpperCase(), parent, page * rows, rows
							* (page - 1));
	}

	@Override
	public List<SysFunction> queryByPageType(long page, long rows, String type)
			throws SQLException {
		return super
				.query(DBService
						.toOraclePage(
								"select A.CODE,A.URL,A.AUTH,A.\"MODE\",A.NAME,A.STATUS,A.CREATE_TIME,A.SORT,B.NAME AS PARENT   from Sys_Function A LEFT  JOIN SYS_FUNCTION B ON B.CODE = A.PARENT  WHERE A.type = ? order by  A.PARENT DESC,A.sort DESC,A.code ,A.create_time desc")
						.toUpperCase(), type, page * rows, rows * (page - 1));
	}

	public List<SysFunction> queryByPageType(long page, long rows, String type,
			String parent) throws SQLException {
		return super
				.query(DBService
						.toOraclePage(
								"select A.CODE,A.URL,A.AUTH,A.\"MODE\",A.NAME,A.PARENT,A.STATUS,A.CREATE_TIME,A.SORT,B.NAME AS PARENT2   from Sys_Function A LEFT  JOIN SYS_FUNCTION B ON B.CODE = A.PARENT  WHERE A.type = ?  AND A.PARENT =  ? order by  A.PARENT DESC,A.sort DESC,A.code ,A.create_time desc")
						.toUpperCase(), type, parent, page * rows, rows
						* (page - 1));
	}

	public boolean update(SysFunction obj) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(SysFunction.TABLE_NAME);
		sb.addField("\"name\"", obj.getName());
		if ("".equals(obj.getParent()))
			obj.setParent(null);
		sb.addField("\"parent\"", obj.getParent());
		sb.addField("\"url\"", obj.getUrl());
		sb.addField("\"auth\"", obj.getAuth());
		sb.addField("\"type\"", obj.getType());
		sb.addField("\"level\"", obj.getLevel());
		sb.addField("\"level_code\"", obj.getLevel_code());
		sb.addField("\"mode\"", obj.getMode());
		sb.addField("\"sort\"", obj.getSort());
		sb.addField("\"status\"", obj.getStatus());
		sb.addWhere("\"code\"", obj.getCode());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean create(SysFunction obj) throws SQLException {
		SqlBuilder sb = SqlBuilder.createInsert(SysFunction.TABLE_NAME);
		sb.addField("\"code\"", obj.getCode());
		sb.addField("\"name\"", obj.getName());
		if ("".equals(obj.getParent()))
			obj.setParent(null);
		sb.addField("\"parent\"", obj.getParent());
		sb.addField("\"url\"", obj.getUrl());
		sb.addField("\"auth\"", obj.getAuth());
		sb.addField("\"type\"", obj.getType());
		sb.addField("\"level\"", obj.getLevel());
		sb.addField("\"level_code\"", obj.getLevel_code());
		sb.addField("\"mode\"", obj.getMode());
		sb.addField("\"sort\"", obj.getSort());
		sb.addField("\"status\"", obj.getStatus());
		sb.addField("\"create_time\"", DateUtil.currentTime());
		StringBuffer sv = new StringBuffer(
				"INSERT INTO SYS_FUNCTION (\"CODE\",\"NAME\",\"PARENT\",\"URL\",\"AUTH\",\"TYPE\",\"LEVEL\",\"LEVEL_CODE\",\"MODE\",\"SORT\",\"STATUS\",\"CREATE_TIME\") VALUES(?,?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))");
		return super.update(sv.toString().toUpperCase(), sb.getParams());
	}

	
}
